package com.farben.web.controller.utils.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class RegexUtils {
  public static int matchPattern(String origin, String regex, int groupIndex) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(origin);
    return Stream.iterate(1, i -> i++)
        .limit(groupIndex)
        .map(i -> matcher.find())
        .map(i -> Integer.parseInt(matcher.group()))
        .collect(Collectors.toList())
        .get(groupIndex - 1);
  }
}
